<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/resources/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/resources/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/resources/css/public.css" media="all">
</head>
<body>
<!-- 查询条件开始 -->
<blockquote class="layui-elem-quote">
    <form class="layui-form" id="searchFrm" lay-filter="searchFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="请输入标题" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <input type="button" value="查询" class="layui-btn" lay-submit id="doSearch" lay-filter="doSearch">
                <input type="reset" value="重置" class="layui-btn layui-btn-danger">
            </div>
        </div>
    </form>
</blockquote>
<!-- 查询条件结束-->

<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="noticeTable" lay-filter="noticeTable"></table>
    <script type="text/html" id="noticeTableToolbar">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </script>
    <script type="text/html" id="noticeTableBar">
        <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
</div>
<!-- 数据表格结束-->

<!-- 弹出层开始 -->
<div  id="addOrUpdateForm" style="display: none;margin: 5px">
    <form class="layui-form" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                 <input type="hidden" name="id">
                <input type="text" name="title" lay-verify="required" placeholder="请输入标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <div id="content" nonce="content" style="margin: 50px 0 50px 0">
                    <p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <input type="button" value="提交" class="layui-btn" lay-submit id="doSubmit" lay-filter="doSubmit">
            <input type="reset" value="重置" id="doReset" class="layui-btn layui-btn-danger">
        </div>
    </form>
</div>
<!-- 弹出层结束-->

</body>
<script src="/resources/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['jquery','table','layer','form','wangEditor'],function () {
        let $=layui.jquery;
        let table=layui.table;
        let layer=layui.layer;
        let form=layui.form;
        let wangEditor=layui.wangEditor;
        let content = new wangEditor('#content');
        content.create();
        //渲染数据表格
        let noticeTable=table.render({
            elem: '#noticeTable'
            ,url:'/notice/list.do'
            ,toolbar: '#noticeTableToolbar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,title: '公告数据表' //导出excel的文件名
            ,cols: [ [
                {field:'id', title:'ID',align:"center"}
                ,{field:'title', title:'标题',align:"center"}
                ,{field:'content', title:'内容',align:"center"}
                ,{field:'createTime', title:'创建时间',align:"center"}
                ,{fixed: 'right', title:'操作', toolbar: '#noticeTableBar',width:'260',align:"center"}
            ] ]
            ,page: true
        });
        //监听模糊查询
        form.on("submit(doSearch)",function (obj) {
            console.log(obj);
            let data=obj.field;
            //刷新表格把数据传过去
            noticeTable.reload({
                where:data
            })
            return false;
        })
        //监听表头的事件
        table.on("toolbar(noticeTable)",function (obj) {
            console.log(obj);
            switch (obj.event){
                case 'add':
                    openAddLayer();//打开添加的弹出层
                    break;
            }
        });
        //监听行工具条的事件
        table.on('tool(noticeTable)', function(obj){
            switch (obj.event){
                case 'update':
                    openUpdateLayer(obj.data)
                    break;
                case 'del':
                    layer.confirm("你确定要删除【"+obj.data.title+"】这个公告吗?",{icon: 3, title:'提示'},function () {
                        $.post("/notice/deleteNoticeById.do",{id:obj.data.id},function (res) {
                            layer.msg(res.msg);
                            if(res.code==200){
                                noticeTable.reload();
                            }
                        })
                    })
                    break;
            }
        });
        
        //打开添加的弹出层的方法
        let url; //提交的URL
        let mainIndex; //当前弹出层的index
        function openAddLayer() {
            mainIndex=layer.open({
                type: 1,//页面层
                title:'添加公告',
                content:$("#addOrUpdateForm"),
                area:['800px','600px'],
                success:function () {
                    $("#doReset").click();
                    url="/notice/addNotice.do";
                }
            })
        }

        //打开修改的弹出层的方法
        function openUpdateLayer(data) {
            mainIndex=layer.open({
                type: 1,//页面层
                title:'修改['+data.title+']公告',
                content:$("#addOrUpdateForm"),
                area:['800px','600px'],
                success:function (){
                    //回显数据
                    form.val('dataFrm',data);
                    url="/notice/updateNotice.do";
                }
            })
        }
        //监听提交事件
        form.on("submit(doSubmit)",function (obj) {
            let data=obj.field;
            //提交
            $.post(url,data,function (res) {
                layer.msg(res.msg);
                if(res.code==200){
                    //刷新表格
                    noticeTable.reload();
                    //关闭弹出层
                    layer.close(mainIndex);
                }
            })
            return false;
        })
    })
</script>
</html>
